﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Inspinia_MVC5.Data;
using Inspinia_MVC5.Models;
using Webdiyer.WebControls.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AdminAuthorize]
    public class StudentsController : Controller
    {
        private BooksSystemContext db = new BooksSystemContext();

        // GET: /Students/
        public ActionResult Index(string name, string idcard,int id=1)
        {
            var students = db.Students.Include(t => t.Class);
            if (name != null)
            {
                students = students.Where(x => x.StuName.Contains(name));
            }
            if (idcard != null)
            {
                students = students.Where(x => x.StuIdCard.Contains(idcard));
            }
            var model = students.OrderByDescending(a => a.ID).ToPagedList(id, 10);
            if (Request.IsAjaxRequest())
                return PartialView("_StudentList", model);
            return View(model);
            
        }

        // GET: /Students/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        // GET: /Students/Create
        public ActionResult Create()
        {
            ViewBag.ClassId = new SelectList(db.Classes, "ClassId", "ClassName");
            return View();
        }

        // POST: /Students/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,StuName,ClassId,StuNum,StuIdCard,StuPhone,StuPwd,Status")] Student student)
        {
            
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase f = Request.Files["photo"];
                    if (f.ContentLength > 0)
                    {
                        string finename = @"/Photo/" + student.StuIdCard + f.FileName.Substring(f.FileName.LastIndexOf('.'));
                        f.SaveAs(Server.MapPath(finename));
                        student.Photo = finename;

                    }
                }
                student.Status = 1;
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ClassId = new SelectList(db.Classes, "ClassId", "ClassName", student.ClassId);
            return View(student);
        }

        // GET: /Students/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            var select = new List<SelectListItem> {
                new SelectListItem {Text="正常",Value="1" },
                new SelectListItem {Text="休学",Value="2" },
                new SelectListItem {Text="毕业",Value="3" }

        };
            ViewBag.select = select;
            ViewBag.ClassId = new SelectList(db.Classes, "ClassId", "ClassName", student.ClassId);
            return View(student);
        }

        // POST: /Students/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,StuName,ClassId,StuNum,StuIdCard,StuPhone,StuPwd,Status")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Entry(student).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ClassId = new SelectList(db.Classes, "ClassId", "ClassName", student.ClassId);
            return View(student);
        }

        // GET: /Students/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }

        // POST: /Students/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Student student = db.Students.Find(id);
            db.Students.Remove(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
